﻿using MiniBottles.Migrations;
using MiniBottles.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MiniBottles
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
           // System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");
           // System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();


            Database.SetInitializer<DataContext>(null);
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
            //using (var ctx = new DataContext())
            //{
            //    ctx.Database.Initialize(force: true);
            //}
            
        }
        void Session_Start(object sender, EventArgs e)
        {
            var db = new DataContext();
            Visitor visitor = new Visitor();
            visitor.IP = Request.UserHostAddress;
            db.Visitors.Add(visitor);
            db.SaveChanges();
        }
        
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
                //It's important to check whether session object is ready
                if (HttpContext.Current.Session != null)
                {
                    CultureInfo ci = (CultureInfo)this.Session["Culture"];
                    //Checking first if there is no value in session 
                    //and set default language 
                    //this can happen for first user's request
                    if (ci == null)
                    {
                        //Sets default culture to english invariant
                        string langName = "pl-PL";
    
                        //Try to get values from Accept lang HTTP header
                        //if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                        //{
                        //    //Gets accepted list 
                        //    langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
                        //}
                            ci = new CultureInfo(langName);
                            this.Session["Culture"] = ci;
                    }
                    //Finally setting culture for each request
                    Thread.CurrentThread.CurrentUICulture = ci;
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
                }
        }
      
       
         }
}